<?php
/**
 * File: Auth.php:YCMS
 * User: xiaoyan[f@yanyunfeng.com]
 * Date: 15-5-6
 * Time: 下午3:58
 * @Description
 */
class UserAuth extends  BaseController{

    public function init()
    {
        $this->addRoute('/group.list','index');
        $this->addRoute('/rule.html','rule');
        $this->addRoute('/auth.rule.add','addRule');
        $this->addRoute('/rule.exists','checkRule');
        $this->addRoute('/auth.group','access');
        $this->addRoute('/auth.group.setAccess','setAccess');
    }
    public function checkRule(){
        $checkData = $this->checkDataNull(array(
            array('name',1,'请输入资源标识')
        ));
        $ruleData = DB()->table('auth_rule')
            ->field('name')
            ->where($checkData)
            ->get();
        if($ruleData){
            ajaxResponse(2,'该资源已经存在了');
        }
        ajaxSuccess();
    }
    public function index(){
        $groupList = DB()->table('auth_group')->select();
        $this->render('auth.index',array(
            'groupList' => $groupList
        ));
    }

    public function rule(){
        $this->render('auth.rule');
    }

    public function addRule(){
        if(isAjax() && $this->input()->post('name')){ //新增
            $saveData = $this->checkDataNull(array(
                array('name',1,'请输入资源标识'),
                array('title',2,'请输入资源名称')
            ));

            $ruleData = DB()->table('auth_rule')
                ->field('name')
                ->where('name',$saveData['name'])
                ->get();
            if($ruleData){
                ajaxResponse(3,'资源标识已经存在了',$ruleData);
            }
            $this->getPostData($saveData,'type,status,condition,parent');
            $saveData['`condition`'] = $saveData['condition'];
            $saveData['`name`'] = $saveData['name'];
            unset($saveData['condition']);
            unset($saveData['name']);
            $this->ajaxBasicInsert('auth_rule',$saveData,false,false);
        }
        $rules = DB()->table('auth_rule')->select();
        $rules = buildTree($rules);
        $this->render('auth.addRule',array(
            'rules' => $rules
        ));
    }

    public function access(){
        $gid = $this->getInput()->get('gid');
        if(!$gid){$this->message('哦~,说好的分组呢');}
        $groupData = DB()->table('auth_group')->where('id',$gid)->get();
        if(!$groupData){$this->message('哦~，好像没有这个分组的说');}
        $rules = DB()->table('auth_rule')->select();
        $groupRule = $groupData['rules'] ? explode(',',$groupData['rules']): array();
        foreach($rules as $i => $r){
            $rules[$i]['checked'] = in_array($r['id'],$groupRule) ? true : false;
        }
        $rules = buildTree($rules);
        $this->render('auth.access',array(
            'rules' => $rules,
            'group' => $groupData
        ));
    }

    public function setAccess(){
        $gid = $this->getInput()->post('gid');
        $rules = $this->getInput()->post('rules');
        if(!$gid){$this->message('哦~,说好的分组呢',null,false,1);}
        $groupData = DB()->table('auth_group')->where('id',$gid)->get();
        if(!$groupData){$this->message('哦~，好像没有这个分组的说',null,false,2);}
        try{
            DB()->update('auth_group',array('rules'=>implode(',',$rules)),array('id'=>$gid));
            $this->message('更新权限成功','group.list',false,0);
        }catch (DBException $e){
            $this->message('哎呀,更新权限失败了',null,false,4);
        }
    }
}